const Koa = require('koa2');
const koa = new Koa();
// const server = require('http').Server(app);

var app = require('koa-websocket')(koa);

const cors = require('koa-cors');
const routes = require('./routers/index');
const { port } = require('./config');

app.use(cors());
routes(app);

let ctxs = [];

app.listen(port, () => {
    console.log(`程序运行在${port}`);
});

app.ws.use((ctx, next) => {
    ctxs.push(ctx);
    ctx.websocket.on("message", (message) => {
        console.log(message);
        // for (let i = 0; i < ctxs.length; i++) {
        //     if (ctx == ctxs[i]) continue;
        //     ctxs[i].websocket.send(message);
        // }
    });
});